package com.geetour.columbus.core.servlets;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Line2D;
import java.awt.image.BufferedImage;
import java.util.Random;

public class CheckCode {
	public static final String Characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

	/**
	 * 创建一个随机数对象
	 */
	Random random = new Random();

	private String codeString;

	/**
	 * 生成图片的宽度
	 */
	private int width;
	/**
	 * 生成图片的高度
	 */
	private int height;

	/**
	 * 字体大小
	 */
	private int fontSize;

	/**
	 * 干扰线数量
	 */
	private int lineCount;

	/**
	 * 设置图片的类型,默认为BufferedImage.TYPE_INT_RGB
	 */
	private int imageType = BufferedImage.TYPE_INT_RGB;

	public int getLineCount() {
		return lineCount;
	}

	public void setLineCount(int lineCount) {
		this.lineCount = lineCount;
	}

	public int getFontSize() {
		return fontSize;
	}

	public void setFontSize(int fontSize) {
		this.fontSize = fontSize;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getImageType() {
		return imageType;
	}

	public void setImageType(int imageType) {
		this.imageType = imageType;
	}

	public String getCodeString() {
		return codeString;
	}

	public void setCodeString(String codeString) {
		this.codeString = codeString;
	}

	/**
	 * 生成4个字符的验证码
	 *
	 * @return
	 */
	public BufferedImage get4Chars() {

		initProperties(90, 15, 17, 10);

		BufferedImage image = new BufferedImage(this.width, this.height,
				this.imageType);
		Graphics g = image.getGraphics();
		Graphics2D g2d = (Graphics2D) g;
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, this.width, this.height);
		g.setFont(new Font("Times New Roman", Font.BOLD, this.fontSize));
		g.setColor(this.getColor(180, 200));

		drawLines(g);

		StringBuffer codeSb = new StringBuffer();
		for (int i = 0; i < 4; i++) {
			char c = Characters.charAt(random.nextInt(Characters.length()));
			codeSb.append(c);
			Color color = new Color(20 + random.nextInt(110),
					20 + random.nextInt(110), 20 + random.nextInt(110));
			g.setColor(color);

			AffineTransform trans = new AffineTransform();
			/*
			 * int x = random.nextInt(3); int y = 0; switch(x){ case 0: y = 5;
			 * break; case 1: y = 10; break; case 2: y = 15; break; default: y =
			 * 10; break; } trans.translate(x, y);
			 */

			float scaleSize = (random.nextFloat() - 0.5f) * 0.2f + 1f;
			trans.scale(scaleSize, scaleSize);

			g2d.setTransform(trans);

			g.drawString(String.valueOf(c), 18 * i + 2, 12);
		}
		this.codeString = codeSb.toString();

		g.dispose();

		return image;
	}

	/**
	 * 生成3个字符的验证码
	 *
	 * @return
	 */
	public BufferedImage get3Chars() {

		initProperties(45, 15, 15, 5);

		BufferedImage image = new BufferedImage(this.width, this.height,
				this.imageType);
		Graphics g = image.getGraphics();

		g.setColor(Color.WHITE);
		g.fillRect(0, 0, this.width, this.height);
		g.setFont(new Font("Times New Roman", Font.BOLD, this.fontSize));
		g.setColor(this.getColor(180, 200));

		drawLines(g);

		StringBuffer codeSb = new StringBuffer();
		for (int i = 0; i < 3; i++) {
			char c = Characters.charAt(random.nextInt(Characters.length()));
			codeSb.append(c);
			Color color = new Color(20 + random.nextInt(110),
					20 + random.nextInt(110), 20 + random.nextInt(110));
			g.setColor(color);

			g.drawString(String.valueOf(c), 15 * i + 2, 12);

		}
		codeString = codeSb.toString();
		g.dispose();

		return image;
	}

	/**
	 * 利用随机数，随机生成一个Color颜色的对象
	 *
	 * @param fc
	 * @param bc
	 * @return颜色对象
	 */
	private Color getColor(int fc, int bc) {
		if (fc > 255) {
			fc = 255;
		}
		if (bc > 255) {
			bc = 255;
		}
		int r = fc + random.nextInt(bc - fc);
		int g = fc + random.nextInt(bc - fc);
		int b = fc + random.nextInt(bc - fc);
		return new Color(r, g, b);
	}

	/**
	 * 生成干扰线
	 *
	 * @param graphics
	 */
	private void drawLines(Graphics graphics) {

		Graphics2D g2d = (Graphics2D) graphics;
		for (int i = 0; i < this.lineCount; i++) {
			int x1 = random.nextInt(this.width);
			int y1 = random.nextInt(this.height);
			int x2 = random.nextInt(this.width - 3);
			int y2 = random.nextInt(this.height - 3);
			BasicStroke bs = new BasicStroke(2f, BasicStroke.CAP_BUTT,
					BasicStroke.JOIN_BEVEL);
			Line2D line = new Line2D.Double(x1, y1, x2, y2);
			g2d.setStroke(bs);
			g2d.draw(line);
			graphics.setColor(getColor(180, 222));
		}
	}

	/**
	 * 初始化个属性
	 *
	 * @param width
	 * @param height
	 * @param fontSize
	 * @param lineCount
	 */
	private void initProperties(int width, int height, int fontSize,
			int lineCount) {
		this.width = width;
		this.height = height;
		this.fontSize = fontSize;
		this.lineCount = lineCount;
	}
}
